const baseUrl = 'https://www.qiyuchuhai.com/xcx'

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function createPubParam() {
  var date = new Date()
  var reqNo = 'qych' + date.getTime()
  var reqTime = date.getFullYear() + '/' + date.getMonth() + '/' + date.getDate() + '-' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
  return { 'reqNo': reqNo, 'reqTime': reqTime }
}

function myRequest(url,params,suc_callback,fail_callback){
  const DC = new createPubParam()
  let data = Object.assign({
    reqNo: DC.reqNo,
    reqTime: DC.reqTime,
    productCode: 600006,
  }, params)

  // wx.showLoading({
  //   title: '加载中..',
  // })

  wx.request({
    url: baseUrl + url,
    data: data,
    method: 'POST',
 
    success: function (res) {
      suc_callback(res.data.data)
      // wx.hideLoading()
    },

    fail: function (res) {
        console.log(baseUrl + url)
     fail_callback?fail_callback(res):''
     console.log('fail')
      console.log(res)
      wx.showToast({
        title: '数据出错!',
        icon: 'none',
        image: '',
        duration: 0,
        mask: true,
      })
    },
  })
}

//点击间隔
function click_wait(e){
  var clickable = getApp().globalData.clickable
  
  if (!getApp().globalData.clickable){
    return
  }
  getApp().globalData.clickable = false
  setTimeout(function(){
    getApp().globalData.clickable = true
  },2000)

  return clickable
}

module.exports = {
  formatTime: formatTime,
  request: myRequest,
  clickable: click_wait,
}
